package com.starry.module.member.core.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.starry.core.common.execption.ServiceException;
import com.starry.core.security.context.SecurityInfoContext;
import com.starry.module.member.core.member.entity.MemberUser;
import com.starry.module.member.core.member.mapper.MemberUserMapper;
import com.starry.module.member.core.member.service.AppMemberUserService;
import com.starry.module.system.api.member.domain.vo.MemberUserLoginInfo;
import org.springframework.stereotype.Service;

/**
 * App会员用户-服务实现
 *
 * @author xiaoke
 */
@Service("appMemberUserService")
public class AppMemberUserServiceImpl implements AppMemberUserService {

    private final MemberUserMapper memberUserMapper;

    public AppMemberUserServiceImpl(MemberUserMapper memberUserMapper) {
        this.memberUserMapper = memberUserMapper;
    }

    @Override
    public MemberUserLoginInfo getByLoginInfo() {
        Long userId = SecurityInfoContext.getUserId();
        if (userId == null) {
            throw new ServiceException("会员未登录");
        }
        MemberUser memberUser = memberUserMapper.selectById(userId);
        if (memberUser == null) {
            throw new ServiceException("会员不存在");
        }
        return BeanUtil.toBean(memberUser, MemberUserLoginInfo.class);
    }
}
